package com.wyl.auth.config.security;

import springfox.documentation.spi.service.contexts.SecurityContext;

/**
 * @Description
 * @Author WuYiLong
 * @Date 2025/3/23 18:43
 */
public class SecurityHolder {

    private static final ThreadLocal<SecurityUser> contextThreadLocal = new ThreadLocal<>();

    public static void setUser(SecurityUser user) {
        if (user == null) {
            clear();
        } else {
            contextThreadLocal.set(user);
        }
    }

    public static SecurityUser getUser() {
        return contextThreadLocal.get();
    }

    public static Long getUserId() {
        SecurityUser user = getUser();
        return user != null ? user.getUserId() : null;
    }

    public static void clear() {
        contextThreadLocal.remove();
    }
}